package chat.fragments;

import android.app.DialogFragment;
import android.app.Fragment;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;


import com.xkh.xkh.R;

import java.io.File;

import chat.activities.CreateRoomActivity;
import chat.activities.NewFriendRequest;
import chat.activities.SearchRoomActivity;
import chat.network.http.UploadAvar;

/**
 * Created by 凯 on 2015/10/26.
 */
public class ControlFragment extends Fragment {

    Delegate delegate;
    View v_addFriend;

     View v_dealrequest;
     View v_createGroup;
    View v_searchRoom;

//    View v_takeAvar;// 获取头像

    TextView tv_newfriendrequestCount;

    private static final int PHOTO_PICKED_WITH_DATA = 3021;
    private static final int CAMERA_WITH_DATA = 3023;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        delegate = (Delegate) getActivity();
        View view = inflater.inflate(R.layout.fragment_control_im, container, false);
        initView(view);
        return view;
    }

    // 点击选项
    View.OnClickListener ItemClick = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (v == v_addFriend) {
                DialogFragment dialog = new AddFriendFragment();
                dialog.show(getFragmentManager(), AddFriendFragment.TAG_ADDFRIEND);
            } else if (v == v_dealrequest) {

                Intent intent = new Intent(getActivity(), NewFriendRequest.class);
                getActivity().startActivity(intent);
            }  else if (v == v_createGroup) {
                Intent intent = new Intent(getActivity(),CreateRoomActivity.class);
                getActivity().startActivity(intent);
            }
//            else if (v == v_takeAvar) {
//                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//                startActivityForResult(intent, CAMERA_WITH_DATA);
//            }
            else if (v == v_searchRoom)
            {
                Intent intent = new Intent(getActivity(), SearchRoomActivity.class);
                getActivity().startActivity(intent);
            }
        }
    };


//    @Override
//    public void onActivityResult(int requestCode, int resultCode, Intent data) {
//        if(resultCode!= Activity.RESULT_OK)
//            return;
//        switch(requestCode){
//            case CAMERA_WITH_DATA:
//                final Bitmap photo = data.getParcelableExtra("data");
//                if(photo!=null){
//                    doCropPhoto(photo);
//                }
//            case PHOTO_PICKED_WITH_DATA:
//                Bitmap photo1 = data.getParcelableExtra("data");
//                if(photo1!=null){
//                    String filePath = Constants.IMAGEDIR+"/"+"avar_"+ UserInfo.getUserName()+".jpg";
//                    DensityUtil.savebitmap2file(photo1,filePath);
//                    new UploadAvarTask().execute(filePath,"avar_"+UserInfo.getUserName()+".jpg");
//                }
//
//        }
//
//    }

    protected void doCropPhoto(Bitmap data){
        Intent intent = getCropImageIntent(data);
        startActivityForResult(intent, PHOTO_PICKED_WITH_DATA);
    }

    public static Intent getCropImageIntent(Bitmap data) {
        Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setType("image/*");
        intent.putExtra("data", data);
        intent.putExtra("crop", "true");
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);
        intent.putExtra("outputX", 128);
        intent.putExtra("outputY", 128);
        intent.putExtra("return-data", true);
        return intent;
    }

    // 初始化控制view
    private void initView(View view) {

        // 添加好友
        v_addFriend = view.findViewById(R.id.id_item_control_addFriend);
        v_addFriend.setOnClickListener(ItemClick);

        // 处理好友请求
        v_dealrequest = view.findViewById(R.id.id_item_control_dealfriend);
        v_dealrequest.setOnClickListener(ItemClick);


        // 创建群组
        v_createGroup = view.findViewById(R.id.id_v_createGroup);
        v_createGroup.setOnClickListener(ItemClick);

        // 新的好友到达提醒
        tv_newfriendrequestCount = (TextView) view.findViewById(R.id.id_tv_newfriendRequestCount);

        // 获取新头像
//        v_takeAvar = view.findViewById(R.id.id_v_takeAvar);
//        v_takeAvar.setOnClickListener(ItemClick);

        v_searchRoom = view.findViewById(R.id.id_v_searchRoom);
        v_searchRoom.setOnClickListener(ItemClick);

    }

    // Activity 回调
    public static interface Delegate {

    }

    // 上传头像的任务
    public class UploadAvarTask extends AsyncTask<Object,Integer,Integer>
    {

        @Override
        protected Integer doInBackground(Object... params) {
            String filePath = (String) params[0];
            String fileName = (String) params[1];

            int isSuccess = 0;
            try {
                if (new UploadAvar(new File(filePath),fileName).upload())
                {
                    isSuccess = 1;
                }
            } catch (Exception e) {
                e.printStackTrace();
                isSuccess = 0;
            }

            return isSuccess;
        }


        @Override
        protected void onPostExecute(Integer integer) {
            if (integer == 1)
            {
                Toast.makeText(ControlFragment.this.getActivity(),"上传成功",Toast.LENGTH_SHORT).show();
            }
        }
    }


}
